package com.jdz.gxq.info.admin.project.service;

import java.util.List;
import java.util.Map;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;

import com.jdz.gxq.info.admin.project.bo.BuildingProject;
import com.jdz.gxq.info.admin.project.bo.ProcessInfo;
import com.jdz.gxq.info.admin.project.bo.Project;

public class BuildingProjectServiceImpl implements BuildingProjectService {
	
	private QueryDAO queryDAO = null;
	
	private UpdateDAO updateDAO = null;
	
	/**
	 * BuildingProject检索分页用，查询记录条数
	 * @return
	 */
	public int getBuildingProjectsCount(String projectName) {
		
		int totalCount = queryDAO.executeForObject("buildingproject.getBuildingProjectsCount",
				projectName, Integer.class);
		
		return totalCount;
	}
	
	/**
	 * BuildingProject检索
	 * @return
	 */
	public List<BuildingProject> getBuildingProjects(Map<String, Object> params) {
		
		return queryDAO.executeForObjectList("buildingproject.getBuildingProjects", params);
	}
	
	/**
	 * 追加BuildingProject
	 * @param buildingproject
	 */
	public void insertBuildingProject(BuildingProject buildingProject) {
		
		// 将状态更新为在建或者待建
		updateDAO.execute("buildingproject.updateProject", buildingProject);

		updateProcessInfos(buildingProject);
		
		updateDAO.execute("buildingproject.insertBuildingProject", buildingProject);
	}
	
	/**
	 * 插入进展情况信息
	 * @param buildingproject
	 */
	private void updateProcessInfos(BuildingProject buildingProject) {
		
		int projectId = buildingProject.getProjectId();
		
		String modifiedBy = buildingProject.getModifiedBy();

		// 先删除，后插入
		deleteProcessInfos(buildingProject.getProjectId());
		
		for (ProcessInfo processInfo : buildingProject.getProcessInfos()) {
			if (processInfo.getProcessDate() != null) {
				processInfo.setProjectId(projectId);
				processInfo.setModifiedBy(modifiedBy);
				updateDAO.execute("buildingproject.insertProcessInfo", processInfo);
			}
		}
	}
	
	private void deleteProcessInfos(int projectId) {
		
		updateDAO.execute("buildingproject.deleteProcessInfos", projectId);
	}
	
	/**
	 * 更新BuildingProject
	 * @param buildingproject
	 */
	public void updateBuildingProject(BuildingProject buildingProject) {
		
		updateDAO.execute("buildingproject.updateProject", buildingProject);
		
		updateProcessInfos(buildingProject);
		
		updateDAO.execute("buildingproject.updateBuildingProject", buildingProject);
	}
	
	/**
	 * 通过id得到buildingproject
	 * @param id
	 * @return
	 */
	public BuildingProject getBuildingProjectById(int projectId) {

		BuildingProject buildingproject = queryDAO.executeForObject(
				"buildingproject.getBuildingProjectById", projectId,
				BuildingProject.class);

		List<ProcessInfo> processInfos = queryDAO.executeForObjectList(
				"buildingproject.getProcessInfos", projectId);

		buildingproject.setProcessInfos(processInfos);

		return buildingproject;
	}
	
	/**
	 * 删除BuildingProject
	 * @param buildingproject
	 */
	public void deleteBuildingProject(BuildingProject buildingProject) {
		
		updateDAO.execute("buildingproject.updateProject", buildingProject);
		
		deleteProcessInfos(buildingProject.getProjectId());
		
		updateDAO.execute("buildingproject.deleteBuildingProjectById", buildingProject.getProjectId());
	}
	
	/**
	 * 验证projectName是否重复
	 * flag: 1 追加时验证
	 * flag: 2 更新时验证
	 */
	public boolean checkBuildingProjectName(BuildingProject pBuildingProject, int flag) {
		
		Project project = queryDAO.executeForObject("buildingproject.getProjectByName",
				pBuildingProject.getProjectName(), Project.class);

		if (project == null) {
			return true;
		}
		
		// 更新验证，查出来的数据是不是本身
		if (flag == 2) {
			
			if (project.getProjectId() == pBuildingProject.getProjectId()) {
				return true;
			}
		}
		
		return false;
	}

	public void setQueryDAO(QueryDAO queryDAO) {
		this.queryDAO = queryDAO;
	}

	public void setUpdateDAO(UpdateDAO updateDAO) {
		this.updateDAO = updateDAO;
	}	
}
